import requests
from lxml import etree
import pymysql.cursors


class New():
    def __init__(self):
        self.url = 'https://www.juzikong.com/categories/authors/cate/%E4%B8%BB%E6%8C%81%E4%BA%BA'
        self.headers = {
            'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/75.0.3770.142 Safari/537.36'
        }
        # self.getList(self.url)

    # 获取作者
    def getAuthor(self):
        res = self.commonGet(self.url)
        items = res.xpath(
            '//div[@class="content_2rBSU"]//div[@class="card_1UehP"]')
        print(items)
        for item in items:
            author = item.xpath('.//div[@class="title_2bru9"]//text()')[0]
            link = 'https://www.juzikong.com' + \
                item.xpath('.//div[@class="title_2bru9"]//a/@href')[0]
            self.getList(link, author)
        # 获取列表详情

    def getList(self, url, author):
        response = self.commonGet(url)
        lists = response.xpath(
            '//div[@class="list_s2gkX"]//section[@class="post_3C37i"]')
        for item in lists:
            text = item.xpath('.//div[@class="content_2hYZM"]//span//text()')
            str_con = f'【{author}】{text[0]}'
            with open('语录.txt', 'a', encoding='utf-8') as file:
                file.write(str_con + '\n')
            # text = item.xpath('.//a//span//span//span//text()')[0]
            # book = item.xpath('.//div//a//span//span//text()')
            # book = book[0] if len(book) > 0 else '暂无'
            # print(author, book, text)
            # self.saveMysql(author, book, text)
            # 保存到数据库
        next_url = response.xpath('//button[@class="btn-next"]//a/@href')
        if next_url and len(next_url) > 0:
            next_url = 'https://www.juzikong.com' + next_url[0]
            self.getList(next_url, author)

    def commonGet(self, url):
        res = requests.get(url, headers=self.headers)
        res.encoding = "utf8"
        response = etree.HTML(res.text)
        return response

    def saveMysql(self, author, book, text):
        connection = pymysql.connect(
            host="localhost",
            user="root",
            password="root",
            db="test",
            charset="utf8mb4")
        try:
            # 获取对话指针
            with connection.cursor() as cursor:
                # 创建mysql 语句
                sql = "insert into `saying`(`author`,`title`,`saying`) values(%s,%s,%s)"
                # 执行sql
                cursor.execute(sql, (author, book, text))
                # 提交
                connection.commit()
        finally:
            connection.close


if __name__ == "__main__":
    new = New()
    new.getAuthor()
